<?php
/**
 * Note: 游戏服通知
 */

namespace App\Services;

use App\Jobs\GameNotifyJob;
use App\Models\Game\Center\Account;
use App\Models\Game\Center\WithdrawOrder;

class GameNotifyService
{
    private const EVENT_TYPE_TABLE = 'table';
    private const EVENT_TYPE_CMD = 'cmd';

    const MODEL_EVENT_CREATED = 1;
    const MODEL_EVENT_UPDATED = 2;
    const MODEL_EVENT_DELETED = 3;

    const UUID_ACTION_REGISTER = 'register';
    const UUID_ACTION_PAY = 'pay';

    /**
     * 数据表更新
     * @param string $table
     * @param int $id
     * @param int $modelEvent
     * @param array $extend
     * @return mixed
     */
    public static function modelEvent(string $table, int $id, int $modelEvent, array $extend = [])
    {
        $param = [
            $modelEvent,
            $id,
        ];

        return GameNotifyJob::dispatchNow(self::EVENT_TYPE_TABLE, $table, $param, $extend);
    }


    /**
     * 数据表更新 ID批量更新
     * @param string $table
     * @param array $ids
     * @param int $modelEvent
     * @param array $extend
     * @return mixed
     */
    public static function modelMultiEvent(string $table, array $ids, int $modelEvent, array $extend = [])
    {
        $param = [
            $modelEvent,
            $ids,
        ];

        return GameNotifyJob::dispatchNow(self::EVENT_TYPE_TABLE, $table, $param, $extend);
    }

    /**
     * Command 事件
     * @param string $event
     * @param $param
     * @param array $extend
     * @return mixed
     */
    public static function commandEvent(string $event, $param, array $extend = [])
    {
        return GameNotifyJob::dispatchNow(self::EVENT_TYPE_CMD, $event, $param, $extend);
    }

    /**
     * 订单已支付
     * todo 待约定
     * @return mixed
     */
    /*public static function orderPayed()
    {
        return GameNotifyJob::dispatchNow(self::EVENT_TYPE_CMD, 'order', []);
    }*/


    /**
     * 关服
     * @param string $nodeType
     * @param int $nodeId
     * @return mixed
     */
    public static function nodeClose(string $nodeType, int $nodeId)
    {
        return self::commandEvent(__FUNCTION__, [$nodeType, $nodeId]);
    }

    /**
     * 支付成功通知
     * @param $primaryId
     * @return mixed
     */
    /*public static function payOrderSuccess($primaryId)
    {
        return self::commandEvent('payOrderSuccess', [$primaryId]);
    }*/

    /**
     * 删除玩家通知
     * @param $userId
     * @return mixed
     */
    /*public static function deletePlayer($userId)
    {
        return self::commandEvent('deletePlayer', [$userId]);
    }*/

    /**
     * 修改水线
     * @param int $sceneId
     * @param int $type
     * @param int $waterLine
     * @return mixed
     * @throws \Exception
     */
    public static function changeWaterLine(int $sceneId, int $type, int $waterLine)
    {
        return self::commandEvent(__FUNCTION__, [$sceneId, $type, $waterLine]);
    }

    /**
     * 修改奖池
     * @param int $sceneId
     * @param int $type
     * @param int $waterLine
     * @return mixed
     */
    public static function changePrizePool(int $sceneId, int $type, int $waterLine)
    {
        return self::commandEvent(__FUNCTION__, [$sceneId, $type, $waterLine]);
    }

    /**
     * 踢用户下线
     * @param int $userId
     * @param string $reason
     * @return mixed
     */
    public static function kickUser(int $userId, string $reason = '')
    {
        return self::commandEvent(__FUNCTION__, [$userId, $reason]);
    }

    /**
     * 权益表变动
     * @param int $id
     * @param int $status
     * @return mixed
     */
    public static function withdrawOrderUpdate(int $id, int $status)
    {
        return self::commandEvent(__FUNCTION__, [$id, $status]);
    }

    /**
     * 公告跑马灯
     * @param string $content
     * @return mixed
     */
    public static function noticeMarquee(string $content)
    {
        return self::commandEvent('noticeDialog', [$content]);
    }

    /**
     * 修改用户VIP
     * @param int $userId
     * @param int $vip
     * @return mixed
     */
    public static function updateUserVip(int $userId, int $vip)
    {
        return self::commandEvent(__FUNCTION__, [$userId, $vip]);
    }

    /**
     * 修改用户支付限额
     * @param int $userId
     * @param int $limit
     * @return mixed
     */
    public static function updateUserPayLimit(int $userId, int $limit)
    {
        return self::commandEvent(__FUNCTION__, [$userId, $limit]);
    }

    /**
     * 修改用户权益策略
     * @param int $userId
     * @return mixed
     */
    public static function updateUserWithdrawLimit(int $userId)
    {
        return self::commandEvent(__FUNCTION__, [$userId]);
    }

    /**
     * 绑定支付宝通知
     * @param int $userId
     * @param string $alipayUserId
     * @return mixed
     */
    public static function bindAlipay(int $userId, string $alipayUserId)
    {
        return self::commandEvent(__FUNCTION__, [$userId, $alipayUserId]);
    }

    /**
     * 绑定手机通知
     * @param int $userId
     * @return mixed
     */
    public static function bindMobile(int $userId)
    {
        return self::commandEvent(__FUNCTION__, [$userId]);
    }

    /**
     * @param int $userId
     * @param float $p
     * @return mixed
     */
    public static function resetgamenumberII(int $userId, float $p)
    {
        return self::commandEvent(__FUNCTION__, [$userId, $p]);
    }

    /**
     * 异步订单
     * @param WithdrawOrder $withdrawOrder
     * @param bool $isReplaceAccount
     * @return mixed
     */
    public static function withdrawOrderAsync(WithdrawOrder $withdrawOrder, bool $isReplaceAccount = false)
    {
        return self::commandEvent(__FUNCTION__, [
            $withdrawOrder->user_id,
            $withdrawOrder->id,
            $withdrawOrder->package_id,
            $withdrawOrder->status,
            $withdrawOrder->reason,
            $isReplaceAccount,
        ]);
    }

    /**
     * ZFB有修改
     * @param int $playerId
     * @return mixed
     */
    public static function changedmayun(int $playerId)
    {
        return self::commandEvent(__FUNCTION__, [$playerId]);
    }

    /**
     * 客服回复
     * @param int $userId 用户ID
     * @param int $timestamp 时间戳
     * @param string $msg 消息内容
     * @return mixed
     */
    public static function kefuReply(int $userId, int $timestamp, string $msg = '')
    {
        return self::commandEvent(__FUNCTION__, [$userId, $timestamp, $msg]);
    }

    /**
     * 设备行为 激活
     * @param Account $account
     * @return mixed
     */
    public static function uuidActionActive(Account $account)
    {
        return self::commandEvent(__FUNCTION__, [], $account->getRawOriginal());
    }

    /**
     * 设备行为 注册
     * @param Account $account
     * @return mixed
     */
    public static function uuidActionRegister(Account $account)
    {
        return self::commandEvent(__FUNCTION__, [], $account->getRawOriginal());
    }

    /**
     * 设备行为 充值
     * @param Account $account
     * @param int $price
     * @return mixed
     */
    public static function uuidActionPay(Account $account, int $price)
    {
        return self::commandEvent(__FUNCTION__, [], array_merge($account->getRawOriginal(), ['price' => $price]));
    }

    /**
     * 删除
     * @param Account $account
     * @return mixed
     */
    public static function uuidActionDelete(Account $account)
    {
        return self::commandEvent(__FUNCTION__, [], $account->getRawOriginal());
    }

    /**
     * 幸运红钻箱用户
     * @param int $userId
     * @return mixed
     */
    public static function luckyHbPlayer(int $userId)
    {
        return self::commandEvent(__FUNCTION__, [$userId]);
    }
}
